package com.yanqu.road.dao.impl.activity.twins;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.twins.TwinsKungFuData;
import com.yanqu.road.entity.activity.twins.TwinsUserData;
import com.yanqu.road.entity.activity.twins.TwinsUserKungFuData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;

public class TwinsUserDataDaoImpl extends TempDataObjectDao<TwinsUserData> {


    public static String getUserMaxMissionIdSql = "select max(missionId) as missionId from t_u_activity_twins where userId=?";
    public int getUserMaxMissionId(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserMaxMissionIdSql);
            int i = 1;
            call.setLong(i++,userId);
            rs = call.executeQuery();
            if (rs.next()) {
                int value = rs.getInt("missionId");
                return value - 1;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return -1;
    }

    public static String getUserDataSql = "select * from t_u_activity_twins where activityId=? and userId=?";
    public TwinsUserData getUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserDataSql);
            int i = 1;
            call.setInt(i++,activityId);
            call.setLong(i++,userId);
            rs = call.executeQuery();
            if (rs.next()) {
                TwinsUserData data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertUserDataSql = "insert into t_u_activity_twins(" +
            "activityId,userId,maxBool,maxAttack,isEnterMission,missionId" +
            ",missionMaxBool,missionBool,missionAttack,missionMaxAttackValue,refreshTimes,bossId,bossBool" +
            ",bossRage,hp,hpTime,upKungFuList,myKungFuList,emKungFuList,missionKungFuMap,gameBagKungFuMap" +
            ",missionKungFuCombList,ownKungFuMap,unlockCombList,missionRefeshTimes) " +
            " values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(TwinsUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getMaxBool()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getMaxAttack()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isEnterMission()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMissionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getMissionMaxBool()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getMissionBool()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getMissionAttack()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getMissionMaxAttackValue()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRefreshTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBossId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getBossBool()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBossRage()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHp()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getHpTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getUpKungFuList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getMyKungFuList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getEmKungFuList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getMissionKungFuMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getGameBagKungFuMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getMissionKungFuCombList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getOwnKungFuMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getUnlockCombList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMissionRefeshTimes()));


        if (getDb().executeNoneQuery(insertUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUserDataSql = "update t_u_activity_twins set " +
            " maxBool=?,maxAttack=?,isEnterMission=?,missionId=?,missionMaxBool=?,missionBool=?,missionAttack=?,missionMaxAttackValue=?" +
            " ,refreshTimes=?,bossId=?,bossBool=?,bossRage=?,hp=?,hpTime=?,upKungFuList=?,myKungFuList=?" +
            " ,emKungFuList=?,missionKungFuMap=?,gameBagKungFuMap=?,missionKungFuCombList=?,ownKungFuMap=?,unlockCombList=?,missionRefeshTimes=?" +
            " where activityId=? and userId=?";
    @Override
    protected boolean updateData(TwinsUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getMaxBool()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getMaxAttack()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isEnterMission()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMissionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getMissionMaxBool()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getMissionBool()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getMissionAttack()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getMissionMaxAttackValue()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRefreshTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBossId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getBossBool()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBossRage()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHp()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getHpTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getUpKungFuList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getMyKungFuList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getEmKungFuList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getMissionKungFuMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getGameBagKungFuMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getMissionKungFuCombList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getOwnKungFuMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getUnlockCombList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMissionRefeshTimes()));


        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected TwinsUserData parseData(ResultSet rs) throws SQLException {
        TwinsUserData data = new TwinsUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setMaxBool(rs.getString("maxBool"));
        data.setMaxAttack(rs.getString("maxAttack"));
        data.setEnterMission(rs.getBoolean("isEnterMission"));
        data.setMissionId(rs.getInt("missionId"));
        data.setMissionMaxBool(rs.getString("missionMaxBool"));
        data.setMissionBool(rs.getString("missionBool"));
        data.setMissionAttack(rs.getString("missionAttack"));
        data.setMissionMaxAttackValue(rs.getString("missionMaxAttackValue"));
        data.setMissionRefeshTimes(rs.getInt("missionRefeshTimes"));
        data.setRefreshTimes(rs.getInt("refreshTimes"));
        data.setBossId(rs.getInt("bossId"));
        data.setBossBool(rs.getString("bossBool"));
        data.setBossRage(rs.getInt("bossRage"));
        data.setHp(rs.getInt("hp"));
        data.setHpTime(rs.getLong("hpTime"));

        String upKungFuListStr  = rs.getString("upKungFuList");
        if(!StringUtils.isNullOrEmpty(upKungFuListStr)){
            List<TwinsKungFuData> result = JSON.parseObject(upKungFuListStr, new TypeReference<ArrayList<TwinsKungFuData>>() {});
            if(result != null){
                data.setUpKungFuList(result);
            }
        }

        String myKungFuListStr  = rs.getString("myKungFuList");
        if(!StringUtils.isNullOrEmpty(myKungFuListStr)){
            List<TwinsKungFuData> result = JSON.parseObject(myKungFuListStr, new TypeReference<ArrayList<TwinsKungFuData>>() {});
            if(result != null){
                data.setMyKungFuList(result);
            }
        }

        String emKungFuListStr  = rs.getString("emKungFuList");
        if(!StringUtils.isNullOrEmpty(emKungFuListStr)){
            List<TwinsKungFuData> result = JSON.parseObject(emKungFuListStr, new TypeReference<ArrayList<TwinsKungFuData>>() {});
            if(result != null){
                data.setEmKungFuList(result);
            }
        }

        String missionKungFuMapStr  = rs.getString("missionKungFuMap");
        if(!StringUtils.isNullOrEmpty(missionKungFuMapStr)){
            Map<Integer, TwinsUserKungFuData> result = JSON.parseObject(missionKungFuMapStr, new TypeReference<ConcurrentMap<Integer, TwinsUserKungFuData>>() {});
            if(result != null){
                data.setMissionKungFuMap(result);
            }
        }


        String gameBagKungFuMapStr  = rs.getString("gameBagKungFuMap");
        if(!StringUtils.isNullOrEmpty(gameBagKungFuMapStr)){
            Map<Integer, TwinsUserKungFuData> result = JSON.parseObject(gameBagKungFuMapStr, new TypeReference<ConcurrentMap<Integer, TwinsUserKungFuData>>() {});
            if(result != null){
                data.setGameBagKungFuMap(result);
            }
        }


        String missionKungFuCombListStr  = rs.getString("missionKungFuCombList");
        if(!StringUtils.isNullOrEmpty(missionKungFuCombListStr)){
            List<Integer> result = JSON.parseObject(missionKungFuCombListStr, new TypeReference<ArrayList<Integer>>() {});
            if(result != null){
                data.setMissionKungFuCombList(result);
            }
        }

        String ownKungFuMapStr  = rs.getString("ownKungFuMap");
        if(!StringUtils.isNullOrEmpty(ownKungFuMapStr)){
            Map<Integer, TwinsUserKungFuData> result = JSON.parseObject(ownKungFuMapStr, new TypeReference<ConcurrentMap<Integer, TwinsUserKungFuData>>() {});
            if(result != null){
                data.setOwnKungFuMap(result);
            }
        }

        String unlockCombListStr  = rs.getString("unlockCombList");
        if(!StringUtils.isNullOrEmpty(missionKungFuCombListStr)){
            List<Integer> result = JSON.parseObject(unlockCombListStr, new TypeReference<ArrayList<Integer>>() {});
            if(result != null){
                data.setUnlockCombList(result);
            }
        }

        return data;
    }

}
